﻿using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using wt.wine.Application.Common.Interfaces;
using wt.wine.Application.Common.Models;

namespace wt.wine.Application.ManageDevicetable.Queries
{
    public class GetDevicetableNameStateusQueries : IRequest<Json<List<GetDevicetableNameStateusDTO>>>
    {
        public List<string> list { get; set; }
        public int type { get; set; }
    }

    public class GetDevicetableNameStateusQueriesHandler : IRequestHandler<GetDevicetableNameStateusQueries, Json<List<GetDevicetableNameStateusDTO>>>
    {
        private readonly IGetDevocetableStatusRepository _igetDevocetableStatusRepository;
        private readonly IDevicetableRepository _idevicetableRepository;
        public GetDevicetableNameStateusQueriesHandler(IGetDevocetableStatusRepository igetDevocetableStatusRepository, IDevicetableRepository idevicetableRepository)
        {
            _igetDevocetableStatusRepository = igetDevocetableStatusRepository;
            _idevicetableRepository = idevicetableRepository;
        }
        public async Task<Json<List<GetDevicetableNameStateusDTO>>> Handle(GetDevicetableNameStateusQueries request, CancellationToken cancellationToken)
        {
            List<GetDevicetableNameStateusDTO> list = new List<GetDevicetableNameStateusDTO>() { };

            if (request != null && request.list != null && request.list.Any())
            {
                //var list_result = await _igetDevocetableStatusRepository.GetDeviceStates(cancellationToken);
                DateTime dt_now = DateTime.Now.AddMinutes(-3);
                var list_result = await _idevicetableRepository.GetDeviceList(request.list, dt_now, cancellationToken);

                for (int i = 0; i < request.list.Count(); i++)
                {
                    if (list_result != null && list_result.Count() > 0)
                    {
                        var model = list_result.Where(p => p.deviceid == request.list[i]).FirstOrDefault();
                        if (model != null && model.id > 0)
                        {
                            list.Add(new GetDevicetableNameStateusDTO() { deviceid = model.deviceid, state = model.state });
                        }
                        else
                        {
                            list.Add(new GetDevicetableNameStateusDTO() { deviceid = request.list[i], state = 0 });
                        }
                    }
                    else
                    {
                        list.Add(new GetDevicetableNameStateusDTO() { deviceid = request.list[i], state = 0 });
                    }
                }

                if (list_result != null && list_result.Any())
                {
                    foreach (var item in list_result)
                    {
                        list.Add(new GetDevicetableNameStateusDTO() { deviceid = item.deviceid, state = item.state });
                    }
                }
                return new Json<List<GetDevicetableNameStateusDTO>>() { Status = 1000, Detail = "查询成功", Data = list };
            }
            else
            {
                return new Json<List<GetDevicetableNameStateusDTO>>() { Status = 1003, Detail = "参数不能为空", Data = list };
            }
        }

        public int GetStateName(int State, int OOWState)
        {
            //-1 设备 不明 0 离线   1 在线 且空闲 2、在线 出酒中 3、在线 已暂停,. 
            if (State == 0)
            {
                return 0;
            }
            else if (State == 1)
            {
                ///出酒状态   0:待出酒   1：出酒中   2：已暂停
                switch (OOWState)
                {
                    case 0:
                        return 1;
                    case 1:
                        return 2;
                    case 2:
                        return 3;
                    default:
                        break;
                }
            }
            return -1;
        }
    }
}
